#include "modulehplcpump.h"
#include  <QDebug>
#include "modulehplcpumpparamiddefine.h"
#include "modulehplcdefine.h"
#include "modulehplccmdid.h"

ModuleHplcPump::ModuleHplcPump(QObject *parent)
    : BaseModule (parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(50); // BASE_TICK_MS
    connect(m_timer, &QTimer::timeout, this, &ModuleHplcPump::onTimeout);
}

ModuleHplcPump::~ModuleHplcPump()
{
    delete  m_timer;
}

bool ModuleHplcPump::setConfig(int id, QVariant &var)
{

}

bool ModuleHplcPump::getConfig(int id, QVariant &var)
{

}

bool ModuleHplcPump::setParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcPumpParamID_SoftwareVersion:
            return true;
        case ModuleHplcPumpParamID_BasicParam:
            {
                hplc::PumpBasicParam param;
                param = var.value<hplc::PumpBasicParam>();
                m_param.basicParam = param;
            }
            return true;
        case ModuleHplcPumpParamID_FlowCalibData:
            {
                QList<hplc::PumpRateCalibData> list;
                list = var.value<QList<hplc::PumpRateCalibData>>();
                m_param.systemParam.rateCalibTable = list;
            }
            return true;
        case ModuleHplcPumpParamID_PressureCalibData:
            {
                QList<hplc::PumpPressureCalibData> list;
                list = var.value<QList<hplc::PumpPressureCalibData>>();
                m_param.systemParam.pressureCalibTable = list;
            }
            return true;
        case ModuleHplcPumpParamID_PulseCompenCalibData:
            {
                QList<hplc::PumpPulseCompenCalibData> list;
                list = var.value<QList<hplc::PumpPulseCompenCalibData>>();
                m_param.systemParam.pulseCompenCalibTable = list;
            }
            return true;
        case ModuleHplcPumpParamID_StepMotorCurrentCalibData:
            {
                QList<hplc::PumpStepMotorCurrentCalibData> list;
                list = var.value<QList<hplc::PumpStepMotorCurrentCalibData>>();
                m_param.systemParam.stepMotorCurrentCalibTable = list;
            }
            return true;
        default:
            return false;
    }
}

bool ModuleHplcPump::getParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcPumpParamID_SoftwareVersion:
            var.setValue(m_param.softwareVersion);
            return true;
        case ModuleHplcPumpParamID_BasicParam:
            var.setValue(m_param.basicParam);
            return true;
        case ModuleHplcPumpParamID_WorkMode:
            var.setValue(m_param.systemParam.workMode);
            return true;
        case ModuleHplcPumpParamID_FlowCalibData:
            var.setValue(m_param.systemParam.rateCalibTable);
            return true;
        case ModuleHplcPumpParamID_PressureCalibData:
            var.setValue(m_param.systemParam.pressureCalibTable);
            return true;
        case ModuleHplcPumpParamID_PulseCompenCalibData:
            var.setValue(m_param.systemParam.pulseCompenCalibTable);
            return true;
        case ModuleHplcPumpParamID_StepMotorCurrentCalibData:
            var.setValue(m_param.systemParam.stepMotorCurrentCalibTable);
            return true;
        default:
            return false;
    }
}

bool ModuleHplcPump::setProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->setProtocolCmd(id, var);
}

bool ModuleHplcPump::getProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->getProtocolCmd(id, var);
}

void ModuleHplcPump::setPort(BasePort *port)
{
    QMutexLocker locker(m_mutex);
    if (m_port)
    {
        m_port->disconnect();
        m_port->deleteLater();
    }
    m_port = port;

    connect(port, &BasePort::receivePackets,
            this, &ModuleHplcPump::onParse, Qt::QueuedConnection);
    connect(this, &ModuleHplcPump::requestSend,
            port, &BasePort::onSend, Qt::QueuedConnection);
}

BasePort *ModuleHplcPump::getPort(void)
{
    QMutexLocker locker(m_mutex);
    return m_port;
}

void ModuleHplcPump::setProtocol(BaseProtocol *protocol)
{
    QMutexLocker locker(m_mutex);
    if (m_protocol)
    {
        m_protocol->disconnect();
        m_protocol->deleteLater();
    }
    m_protocol = protocol;
    connect(m_protocol, &BaseProtocol::requestSend, this, &ModuleHplcPump::requestSend);
    connect(m_protocol, &BaseProtocol::parseCompleted, this, &ModuleHplcPump::parseCompleted);
}

BaseProtocol *ModuleHplcPump::getProtocol()
{
    QMutexLocker locker(m_mutex);
    return m_protocol;
}

void ModuleHplcPump::setProtocolAddress(quint8 address)
{
    QMutexLocker locker(m_mutex);
    m_protocol->setConfig(0, address);
}

quint8 ModuleHplcPump::getProtocolAddress(void)
{
    QMutexLocker locker(m_mutex);
    QVariant var;
    m_protocol->getConfig(0, var);
    return var.toUInt();
}

hplc::PumpParam *ModuleHplcPump::getParam()
{
    QMutexLocker locker(m_mutex);
    return &m_param;
}

void ModuleHplcPump::onTimeout()
{
    QMutexLocker locker(m_mutex);
    m_protocol->onBaseTickTimeout();
}

void ModuleHplcPump::onParse(QList<ProtocolPacket> packets)
{
    QMutexLocker locker(m_mutex);
    if (m_enable)
        m_protocol->onWork(packets);
}

void ModuleHplcPump::onStart()
{
    QMutexLocker locker(m_mutex);
    if (m_enable && m_port->isOpen())
        m_timer->start();
}
